#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
    printf("测试1:\n");
    pid_t pid = fork();
    if (pid < 0)
        return -1;
    if (pid == 0)
    {
        // while (1)
        //{
        printf("i am child process\n");
        sleep(3);
        // }
        printf("child process quit!\n");
        exit(10);
    }
    else
    {
        int status;
        int ret = wait(&status);
        if (ret > 0 && (status & 0xFF) == 0)
        {
            // 子进程正常退出
            printf("子进程正常退出，子进程退出码：%d\n", (status >> 8) & 0xFF);
        }
        else if (ret > 0)
        {
            // 子进程异常退出
            printf("子进程异常退出，子进程退出信号：%d,coredump:%d", status & 0x7F, (status & 0xFF) >> 7);
        }
    }
    ///////////////////////////////////////////////
    printf("测试2:\n");
    pid = fork();
    if (pid < 0)
        return -1;
    if (pid == 0)
    {
        while (1)
        {
            printf("i am child process\n");
            sleep(3);
        }
        printf("child process quit!\n");
        exit(10);
    }
    else
    {
        int status;
        int ret = wait(&status);
        if (ret > 0 && (status & 0xFF) == 0)
        {
            // 子进程正常退出
            printf("子进程正常退出，子进程退出码：%d\n", (status >> 8) & 0xFF);
        }
        else if (ret > 0)
        {
            // 子进程异常退出
            printf("子进程异常退出，子进程退出信号：%d,coredump:%d\n", status & 0x7F, (status & 0xFF) >> 7);
        }
    }
    return 0;
}